package com.yichi.service;

import android.util.Log;

import com.yichi.global.AppConfig;
import com.yichi.util.Utils;
//import com.yujing.yserialport.YSerialPort;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * ic卡服务
 * 弃用
 */
public class IccardComService {
    private final static String TAG = "IccardService";

    private final static String EMPT_NO = "";//空编号定义
    private final static int TIMEOUT = 20 * 1000;//查询电池编号的超时时间，20秒

    public static IccardComService inst = new IccardComService();
    private IccardComService() {

    }

//    public void init() {
//        this.getInputStoreBatteryNo1();
//    }

    /**
     * 获取存电仓1的电池编号
     *
     * @return
     */
//    public String getInputStoreBatteryNo1() {
//        byte[] bytes = {};
//        return getBatteryNo(bytes, TIMEOUT);
//    }

    /**
     * 获取存电仓2的电池编号
     *
     * @return
     */
//    public String getInputStoreBatteryNo2() {
//        byte[] bytes = {};
//        return getBatteryNo(bytes, TIMEOUT);
//    }

    /**
     * 获取存电仓3的电池编号
     *
     * @return
     */
//    public String getInputStoreBatteryNo3() {
//        byte[] bytes = {};
//        return getBatteryNo(bytes, TIMEOUT);
//    }

    /**
     * 获取存电口的电池编号
     * ic卡串口读取
     *
     * @param bytes
     * @param timeoutMillis
     * @return
     */
//    public String getBatteryNo(byte[] bytes, Integer timeoutMillis) {
//        final CountDownLatch latch = new CountDownLatch(1); // Used to block the main thread
//        final String[] result = {""}; // To store the result (it has to be an array due to lambda limitations)
//
//        // Start a background thread to handle the async task
//        Thread thread = new Thread(() -> {
//            long startTime = System.currentTimeMillis();
//
//            while (System.currentTimeMillis() - startTime < timeoutMillis) {
//                try {
//                    byte[] data = YSerialPort.sendSyncOnce(AppConfig.ICCARD_PORTNAME, AppConfig.ICCARD_BAUDRATE, bytes, 100);
//                    String str = Utils.byteArray2String(data);
//                    Log.i(TAG, "IC Card data received: " + str);
//
//                    //查到了电池编号
//                    if (!str.equalsIgnoreCase(EMPT_NO)) {
//                        result[0] = str; // Set the result to "a" when matched
//                        latch.countDown(); // Signal that the result is ready
//                        return; // Exit the thread
//                    }
//
//                    // Optional: Sleep for a short duration before retrying
//                    Thread.sleep(100); // Sleep for 100ms (adjust as needed)
//
//                } catch (Exception e) {
//                    Log.e(TAG, "IC Card query failed", e);
//                    result[0] = ""; // Set result as empty string on error
//                    latch.countDown(); // Signal completion even if failed
//                    return;
//                }
//            }
//
//            // 超时了
//            Log.e(TAG, "Timeout reached, IC Card query failed.");
//            result[0] = "";
//            latch.countDown(); // Signal completion after timeout
//        });
//
//        thread.start(); // Start the background thread
//
//        try {
//            // Wait for the latch to be counted down (either by success or timeout)
//            boolean completed = latch.await(timeoutMillis, TimeUnit.MILLISECONDS);
//
//            if (!completed) {
//                Log.e(TAG, "Timeout reached before receiving expected data.");
//                return ""; // Timeout occurred, return empty string
//            }
//
//        } catch (InterruptedException e) {
//            Thread.currentThread().interrupt();
//            Log.e(TAG, "Thread was interrupted.", e);
//            return ""; // Handle interruption by returning empty string
//        }
//
//        return result[0]; // Return the result (either "a" or an empty string if failed or timed out)
//    }
}
